<?php
class LoginAction extends CommonAction{
	public function index(){
		$uid		= $_COOKIE[C('COOKIE_PREFIX').'uid'];
		if($uid > 0){
			redirect("/member/");
		}
		$this->display();
	}

	function check(){
		$user	= trim($_POST["user"]);
		$pass	= trim($_POST["pass"]);
		if(empty($user)){
			$this->error("用户名非空");
		}elseif(empty($pass)){
			$this->error("密码非空");
		}

		import ( '@.ORG.RBAC' );
		//生成认证条件
		$map            =   array();
		// 支持使用绑定帐号登录
		$map['user']	= $user;
		$map["status"]	=	array('gt',0);
		$authInfo = RBAC::authenticate($map);
		//使用用户名、密码和状态的方式进行认证
		if(false === $authInfo) {
			$this->error('帐号不存在或已禁用！');
		}else {
			if($authInfo['pass'] != md5($pass)) {
				$this->error('密码错误！');
			}
			cookie(C('USER_AUTH_KEY'),$authInfo['id']);
			cookie("name",$authInfo['name']);
			cookie("user",$authInfo['user']);

			//保存登录信息
			$Member	=	M('Member');
			$ip		=	get_client_ip();
			$time	=	time();
			$data = array();
			$data['id']			=	$authInfo['id'];
			$data['updated']	=	$time;
			$data['login_num']	=	array('exp','login_num+1');
			$data['last_login_ip']	=	$ip;
			$Member->save($data);

			// 缓存访问权限
			RBAC::saveAccessList();
			$this->success('登录成功！');
		}
	}

	function logout(){
		cookie(C('USER_AUTH_KEY'),"");
		cookie("name","");
		$this->assign("jumpUrl",'/');
		$this->success('退出成功！');
	}
}
?>